<?php
/**
 *  Email sending handler
 */

// Sécurité
if (!defined("_ECRIRE_INC_VERSION")) return;

include_spip('functions/pdk_common');

function traiter_add_article_dist($args, $retours){
	$formulaire = $args['formulaire'];
	$options = $args['options'];
	$saisies = unserialize($formulaire['saisies']);
	$traitements = unserialize($formulaire['traitements']);
	$champs = saisies_lister_avec_option('nom',$saisies);
    include_spip('spip_bonux_fonctions');
    $article_rubrique = picker_selected($options['id_rubrique'], 'rubrique');
    
    if (($id_rubrique = $article_rubrique[0]) && $options['statut'])
    {
        $article_mdl = pdk_load_class('PdkArticleModel', 'classes/models');
        
		$article_data = array();
        $article_data['id_rubrique'] = $id_rubrique;
        $article_data['statut'] = $options['statut'];
        $article_data['date'] = date('Y-m-d H:i:s');
        $article_data['titre'] = _request($options['titre_field']);
        $article_data['texte'] = _request($options['texte_field']);
        $article_data['descriptif'] = _request($options['descriptif_field']);
        $article_data['surtitre'] = _request($options['surtitre_field']);
        $article_data['soustitre'] = _request($options['soustitre_field']);
        $article_data['chapo'] = _request($options['chapo_field']);
        $article_data['ps'] = _request($options['ps_field']);
        $article_data['nom_site'] = _request($options['nom_site_field']);
        $article_data['url_site'] = _request($options['url_site_field']);
        $article_data['lang'] = $GLOBALS['meta']['langue_site']; //TODO: Уточнить вопрос с добавлением статей в разных языковых версиях
        
        $id_article = $ok = $article_mdl->insert($article_data);
        
        // Upload logo
        if ($options['logo_field'] && $id_article) {//TODO: Нужно проверять минимальное количество загружаемых файлов
            $pdk_logo = pdk_load_class('pdk_logo', 'classes');
            $pdk_logo->filename = 'arton';
            $pdk_logo->max_size = $champs[$options['logo_field']]['options']['allowed_types'];
            $pdk_logo->logo_input_name = $options['logo_field'].'0';
            /*$ok = */$pdk_logo->upload_logo($id_article);
        }
        
        // Upload documents
        if ($options['documents_field'] && $id_article) {
            $document_mdl = pdk_load_class('pdk_document_model', 'classes');
            $document_mdl->allowed_types = $champs[$options['documents_field']]['options']['allowed_types'];
            $document_mdl->max_size = $champs[$options['documents_field']]['options']['max_size'];
            
            $document_lien_mdl = pdk_load_class('pdk_document_lien_model', 'classes');
            
            $document_lien_data = array('id_objet'=>$id_article,'objet'=>'article');
            
            foreach ($_FILES as $file=>$file_data) {
                if ($file_data['name'] && strstr($file, $options['documents_field'])!==false) {
                    $document_mdl->document_input_name = $file;
                    if ($id_document = $document_mdl->insert(array())) {
                        $document_lien_data['id_document'] = $id_document;
                        $document_lien_mdl->insert($document_lien_data);
                    }
                }
            }
        }
        
        
		if ($ok && strpos($retours['message_ok'], _T('feedback_formidable:traiter_add_article_message_ok')) === FALSE){            
			 $retours['message_ok'] .= "\n<br/>"._T('feedback_formidable:traiter_add_article_message_ok');
		}
		else if (!$ok && strpos($retours['message_erreur'], _T('feedback_formidable:traiter_add_article_message_erreur')) === FALSE) {
			$retours['message_erreur'] .= "\n<br/>"._T('feedback_formidable:traiter_add_article_message_erreur');
		}
	}
	
	return $retours;
}

?>
